<?php
use Wdl\helper\webHelper;
/** 
 * 伍成才技术
 * 微房产中介公司营销房源，目标是可以省去58赶集上 发布房源的费用
 * @author siyueweb   联系QQ34010235
 * @url http://www.siyueweb.cn;
 */
defined('IN_IA') or exit('Access Denied');
define('IMS_FAMILY', 'siyueweb');
 
require IA_ROOT . '/addons/kbwdl_shequzufang/vender/element.class.php';
require IA_ROOT .'/addons/kbwdl_shequzufang/vendor/autoload.php';

class kbwdl_shequzufangModuleSite extends WeModuleSite {
    
    public $settings;
    public $_forms =  null;
    /**
     * 构造函数
     * @global type $_GPC
     * @global type $_W
     */

    public function __construct() {
        global $_GPC, $_W;

        $this->_forms = webHelper::sechouse_form_data();
    //     $sql = 'SELECT `settings` FROM ' . tablename('uni_account_modules') . ' WHERE `uniacid` = :uniacid AND `module` = :module';
    //     $settings = pdo_fetchcolumn($sql, array(':uniacid' => $_W['uniacid'], ':module' => 'kbwdl_shequzufang'));
    //     $adapter =  include  IA_ROOT. "/addons/kbwdl_shequzufang/vender/adapter.php";
      
    //     $this->_forms = $adapter['sechouse'];
    //    /* $this->checksigle();*/
    //     $settings =   iunserializer($settings);
    //      if(isset($settings['charges_category']) && $settings['charges_category']){
    //         $ccstr = trim($settings['charges_category']);
    //         $cc = explode('|', '请选择|'. $ccstr);
    //         unset($cc[0]);
    //         $this->_forms['charges_step'] = $cc;
    //     }
    }
    public function checksigle(){
        global $_GPC, $_W;/**/
        $ret =  pdo_fetchcolumn("select count(*) as num from ".tablename('wxapp_versions')." where modules like '%kbwdl_shequzufang%'");
        if($ret >1){
            $first = pdo_fetchcolumn("select uniacid from ".tablename('wxapp_versions')." where modules like '%kbwdl_shequzufang%' order by id asc");
          
            if($first != $_W['uniacid']){
                message('当前是单开版本，请购买多开版本继续添加多个小程序');
            }   
        } 
         /**/     
    }
    public function module_setting()
    {
        global $_GPC, $_W;
        return webHelper::fetch_place_data('setting','areas_prices');
    }
    // public function module_setting(){
    //     global $_GPC, $_W;
    //     $sql = 'SELECT `settings` FROM ' . tablename('uni_account_modules') . ' WHERE `uniacid` = :uniacid AND `module` = :module';
    //     $settings = pdo_fetchcolumn($sql, array(':uniacid' => $_W['uniacid'], ':module' => 'kbwdl_shequzufang'));
    //     return  iunserializer($settings);
    // }
    /**这里写此方法是为了它的内部不能调用的问题 */
    public function createWebUrl($do, $query = array()) {
		$query['do'] = $do;
		$query['module_name'] = strtolower($this->modulename);

		return wurl('site/entry', $query);
	}

    public function doMobileIndex()
    {
        global $_GPC, $_W;
 
        checkauth();  
$kbwdl_shequzufang_siteinfo = <<<EOD

    window.siteinfo = {
            "uniacid": "{$_W['uniacid']}",
            "acid": "{$_W['acid']}",
            "multiid": "1",
            "version": "1.8.1",
            "siteroot": "{$_W['siteroot']}app/index.php",
            'method_design': '3',
            'session_id': "{$_W['session_id']}",
            'openid': "gzhc_{$_W['openid']}"
    }; 

EOD;
        include $this->template("index");
    }
    public function result($errno, $message, $data = '') {
		exit(json_encode(array(
			'errno' => $errno,
			'message' => $message,
			'data' => $data,
		)));
	}
    /**
     * 新版本采用model扩展代码
     * 重新修改__call方法
     */   
    public function __call($name, $arguments) {
        global $_GPC; 
        $operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
        //查看model类是否纯在
        $function_name = strtolower(substr($name, 8));
        $className = "Wdl\\model\\". strtolower( $function_name );
        if(class_exists($className)){
          
        // 使用反射类获取类的方法信息
            $reflectionClass = new \ReflectionClass($className);
        
            $reflectionMethod = $reflectionClass->getMethod( $operation );
            // 创建类的实例
            $instance = $reflectionClass->newInstanceArgs( [$this] );
            $result =  $reflectionMethod->invoke($instance, $arguments);
            return  $this->result($result['code'], $result['msg'], $result['data']);
            
        }
        //如果model类不存在，采用微擎默认的inc/wxapp/里面的文件
		parent::__call($name,  $arguments );
	}

     /**
     * 新版本采用model扩展代码
     * 重新修改__call方法
     */   
    public function __call2($name, $arguments) {
        //查看model类是否纯在
        global $_GPC;
        $isWeb = 0 === stripos($name, 'doWeb');
		$isMobile = 0 === stripos($name, 'doMobile');
        $operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
        if($isMobile || $isWeb){
            if ($isWeb) {
				$dir  = 'web/';
				$function_name = strtolower(substr($name, 5));
			}
			if ($isMobile) {
				$dir  = 'mobile/';
				$function_name = strtolower(substr($name, 8));
			}
        }
        
        $className = "Wdl\\".trim($dir, "/")."\\controller\\". strtolower( $function_name );
       
        if(class_exists($className)){
          
        // 使用反射类获取类的方法信息
            $reflectionClass = new \ReflectionClass($className);
        
            $reflectionMethod = $reflectionClass->getMethod( $operation );
            // 创建类的实例
            $instance = $reflectionClass->newInstanceArgs( [$this] );
            $result =  $reflectionMethod->invoke($instance, $arguments);
            
            
        }else{
            //如果model类不存在，采用微擎默认的inc/wxapp/里面的文件
            parent::__call($name,  $arguments );
        }
        
	}
}